﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Sockets.Controller;
using Pixysoft.Framework.Reflection;
using Pixysoft.Framework.Verifications;
using Pixysoft.Framework.Sockets.Core;

namespace Pixysoft.Framework.Sockets
{
    public class SocketServer
    {
        public static void Initialize(SocketConfiguration config)
        {
            SettingHelper.Initialize(config);
        }

        public static void Start(int port)
        {
            SessionPoolController.Instance.Start(port);
        }

        public static void Stop()
        {
            SessionPoolController.Instance.Stop();
        }

        public static bool IsConnected()
        {
            return SessionPoolController.Instance.IsConnected;
        }

        public static void SetCodecFactory(ISessionCodecFactory codec)
        {
            SessionPoolController.Instance.SetCodecFactory(codec);
        }

        public static void RegisterHandler(IActionHandler handler)
        {
            ProtocolDeclaration declaration = Pixysoft.Reflection.ReflectHelper.Instance.GetCustomerAttribute<
                ProtocolDeclaration>(handler.GetType());

            if (declaration == null)
                throw Exceptions.ManualVerificationFailedException("no annotaion for " + handler.GetType().Name);

            ActionHandlerFactory.Instance.RegisterHandler(declaration.Protocol, declaration.Action, handler);

        }

        public static void RegisterHandler(int protocol, int action, IActionHandler handler)
        {
            ActionHandlerFactory.Instance.RegisterHandler(protocol, action, handler);
        }

        public static void ClearHandlers()
        {
            ActionHandlerFactory.Instance.ClearHandlers();
        }

        public static IMessageFactory GetMessageFactory()
        {
            return MessageController.Instance;
        }

        public static ISessionFactory GetSessionFactory()
        {
            return SessionPoolController.Instance;
        }

        public static IGlobalPerformance GetGlobalPerformance()
        {
            return GlobalPerformanceAnalyzer.Instance;
        }

    }
}
